Realtime Slider Update
-
Dear community,
I'm trying to scale a cube with a Slider in realtime. But I don't get it.
The cube is only scaled after the mouse is released and not in realtime.
import c4d class SliderDialog(c4d.gui.GeDialog): def __init__(self): super().__init__() self.slider_id = 1001 self.last_value = None def CreateLayout(self): self.SetTitle("Slider") self.AddEditSlider(self.slider_id, c4d.BFH_SCALEFIT, initw=200) self.SetFloat(self.slider_id, 0.0, min=0.0, max=1.0, step=0.01) return True def Command(self, id, msg): if id == self.slider_id: value = self.GetFloat(self.slider_id) print(f"Real-time slider value: {value}") self.UpdateScene(value) return True def Message(self, msg, result): if msg.GetId() == c4d.BFM_ACTION_INDRAG: value = self.GetFloat(self.slider_id) if value != self.last_value: self.last_value = value print(f"Dragging slider value: {value}") self.UpdateScene(value) return super().Message(msg, result) def UpdateScene(self, value): doc = c4d.documents.GetActiveDocument() obj = doc.SearchObject("Cube") if obj: obj[c4d.ID_BASEOBJECT_REL_SCALE, c4d.VECTOR_X] = value c4d.EventAdd() if __name__ == "__main__": doc = c4d.documents.GetActiveDocument() obj = doc.SearchObject("Cube") if not obj: obj = c4d.BaseObject(c4d.Ocube) doc.InsertObject(obj) c4d.EventAdd() dlg = SliderDialog() dlg.Open(c4d.DLG_TYPE_ASYNC)
Thank you in advance for your assistance.
-
SOLVED!
I got it, instead of using c4d.EventAdd(),
what I was searching for is c4d.DrawViews().
Now the Slider works interactive.import c4d class SliderDialog(c4d.gui.GeDialog): def __init__(self): super().__init__() self.slider_id = 1001 self.last_value = None def CreateLayout(self): self.SetTitle("Slider") self.AddEditSlider(self.slider_id, c4d.BFH_SCALEFIT, initw=200) self.SetFloat(self.slider_id, 0.0, min=0.0, max=1.0, step=0.01) return True def Command(self, id, msg): if id == self.slider_id: value = self.GetFloat(self.slider_id) print(f"Real-time slider value: {value}") self.UpdateScene(value) return True def UpdateScene(self, value): doc = c4d.documents.GetActiveDocument() obj = doc.SearchObject("Cube") if obj: obj[c4d.ID_BASEOBJECT_REL_SCALE, c4d.VECTOR_X] = value #c4d.EventAdd() c4d.DrawViews() # That's it if __name__ == "__main__": doc = c4d.documents.GetActiveDocument() obj = doc.SearchObject("Cube") if not obj: obj = c4d.BaseObject(c4d.Ocube) doc.InsertObject(obj) c4d.EventAdd() dlg = SliderDialog() dlg.Open(c4d.DLG_TYPE_ASYNC)
Cheers
-
Hi @shurkan,
Welcome to the Maxon developers forum and its community, it is great to have you with us!
Getting Started
Before creating your next postings, we would recommend making yourself accustomed with our forum and support procedures. You did not do anything wrong, we point all new users to these rules.
- Forum Overview: Provides a broad overview of the fundamental structure and rules of this forum, such as the purpose of the different sub-forums or the fact that we will ban users who engage in hate speech or harassment.
- Support Procedures: Provides a more in detail overview of how we provide technical support for APIs here. This topic will tell you how to ask good questions and limits of our technical support.
- Forum Features: Provides an overview of the technical features of this forum, such as Markdown markup or file uploads.
It is strongly recommended to read the first two topics carefully, especially the section Support Procedures: Asking Questions.
About your First Question
Great that you've managed to find the solution yourself! Thank you in advance for sharing it with the community!
Just a short note about your solution is to be careful with threading, namely, in your case it's expected to execute DrawViewes in the following way:
c4d.DrawViews(c4d.DRAWFLAGS_ONLY_ACTIVE_VIEW | c4d.DRAWFLAGS_NO_THREAD | c4d.DRAWFLAGS_STATICBREAK)
Something similar to this topic here: Troubleshooting Safe Frame Calculations Across Different Takes in Cinema 4D
Cheers,
Ilia